package thinkinginjava.reusing;
//reusing/LoadClass.java
//TIJ4 Chapter Reusing, Exercise 23, page 274
/* Prove that class loading takes place only once. Prove that loading may be caused
* by either the creation of the first instance of the that class of by access of a 
* static member.
*/
import static thinkinginjava.myutil.Print.print;

class A3 {
	static int j = printInit("A3.j initialized");
	static int printInit(String s) {
		print(s);
		return 1;
	}
	A3() { print("A3() constructor"); }	
} 

class B3 extends A3 {
	static int k = printInit("B.k initialized");
	B3() { print("B3() constructor"); }	
}
	
class C3 {
	static int n = printInitC("C3.n initialized");
	static A3 a = new A3();	
	C3() { print("C3() constructor"); }
	static int printInitC(String s) {
		print(s);
		return 1;
	}
}

class D3 {
	D3() { print("D3() constructor"); }
}

public class LoadClass extends B3 {
	static int i = printInit("LoadClass.i initialized");
	LoadClass() { print("LoadClass() constructor"); }
	public static void main(String[] args) {
		// accessing static main causes loading (and initialization)
		// of A, B, & LoadClass
		print("hi");
		// call constructors from loaded classes:
		LoadClass lc = new LoadClass();
		// call to static field loads & initializes C:
		print(C3.a);
		// call to constructor loads D:
		D3 d = new D3();
	}
}
